Documents > Cookbook >Text Document
TextDocument document1=TextDocument.newTextDocument();
document1.save(filePath);
TextDocument documentMedia1=TextDocument.newTextDocument(OdfMediaType.TEXT_MASTER);
TextDocument documentMedia2=TextDocument.newTextMasterDocument();
TextDocument document2=(TextDocument)TextDocument.loadDocument("textdocument.odt");
document2.changeMode(OdfMediaType.TEXT_WEB);
TextDocument src = TextDocument.loadDocument("SourceDocument.odt"); TextDocument target = TextDocument.loadDocument("TargetDocument.odt"); Paragraph p1 = target.getParagraphByIndex(0, true); // insert contents before p1 and copy styles target.insertContentFromDocumentBefore(src, p1, true); // insert contents after p1 and don't copy styles target.insertContentFromDocumentAfter(src, p1, false);
document1.addParagraph(null);
document1.addParagraph("test newParagraph function in textDocument");
para1.appendTextContent("test addText function in textDocument");
HorizontalAlignmentType align = para1.getHorizontalAlignment();
para1.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
Font font = para1.getFont();
font.setFontStyle(StyleTypeDefinitions.FontStyle.ITALIC);
para2.setFont(font);
para2.applyHyperlink(new URI("mailto:daisy@odftoolkit.org"));
para2.appendHyperlink("mail to me",new URI("mailto:daisy@odftoolkit.org"));
para2.addComment("This is a comment for para2", "Simple ODF Commenter");
// isHeading() and getHeadingLevel(); if (para2.isHeading()) {
int headingLevel = para2.getHeadingLevel();
System.out.println("para2 is a heading, its level is " + headingLevel + ".");
}
// applyHeading(), default level is 1. para1.applyHeading();
// applyHeading(), heading level is 3. para1.applyHeading(true, 3);
Section aSection = document1.getSectionByName("ImageSection");
Iterator<Section> sections = document1.getSectionIterator();
while (sections.hasNext()) {
Section theSection = sections.next();
}
String name = aSection.getName();
aSection.setName("NewName");
document1.appendSection(aSection, true);
document1.appendSection(aSection, false);
aSection.remove();
aSection.setProtected(true);
aSection.setProtectedWithPassword("myPassword");
List newList1 = document1.addList();
List newList2 = new List(document1);
Iterator<List> lists = document1.getListIterator();
List list =null;
while (lists.hasNext()) {
list = lists.next();
}
String header = list.getHeader();
list.setHeader("NewHeader");
java.util.List<ListItem> items = list.getItems();
int location = 2;
ListItem item = list.getItem(location);
// add single item ListItem newItem1 = list.addItem(item);
ListItem newItem2 = list.addItem("itemContent");
// add clone items ListItem[] newItems = new ListItem[]{newItem1, newItem2};
list.addItems(newItems);
// add string items String[] newItemContents = new String[]{"itemContent1", "itemContent2"};
list.addItems(newItemContents);
newItem1 = list.addItem(location, item);
newItem2 = list.addItem(location, "itemContent");
list.addItems(location, newItems);
list.addItems(location, newItemContents);
item = list.set(location, newItem1);
item = list.set(location, "itemContent");
list.removeItem(item);
//item in specific location. list.removeItem(location);
//item in specific collection. list.removeItems(items);
// the numbering of the proximate list is continued newList1.setContinueNumbering(true);
// the numbering of the specified list is continued by this list newList2.setContinueList(list);
ListType type = list.getType();
// create a number list. ListDecorator numberDecorator = new NumberDecorator(document1);
List numberList1 = document1.addList(numberDecorator);
List numberList2 = new List(document1, numberDecorator);
list.remove();
document1.removeList(list);
String itemContent = item.getTextContent();
item.setTextContent("new item content");
int index = item.getIndex();
List ownerList = item.getOwnerList();
item.remove();
item.setStartNumber(3);
item.addList();
Header docHeader = doc.getHeader();
Footer footer = doc.getFooter();
Table table = docHeader.addTable(1,2);
table.getCellByPosition(0, 0).setStringValue("header table cell");
Cell cell = table.getCellByPosition(1, 0);
Image image1 = cell.setImage(new URI("file:/c:/image.jpg"));
table = footer.addTable(1, 1);
Cell cellByPosition = table.getCellByPosition(0, 0);
cellByPosition.setStringValue("footer table cell");
cellByPosition.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
Font myFont = new Font("Arial", StyleTypeDefinitions.FontStyle.ITALIC, 12, Color.BLUE);
cellByPosition.setFont(myFont);
cellByPosition.setCellBackgroundColor(Color.YELLOW);
Paragraph paragraph = doc.getParagraphByIndex(0, false);
Textbox box = paragraph.addTextbox(new FrameRectangle(1,1,2,1,SupportedLinearMeasure.IN));
box.setTextContent("this is a text box");
box.setName("MyTextbox");
Textbox myBox=paragraph.getTextboxByName("MyTextbox");
Iterator<Textbox> boxIter = paragraph.getTextboxIterator();
myBox.setBackgroundColor(Color.BLUE);
paragraph.removeTextbox(myBox);
Paragraph para = doc.getParagraphByIndex(1, false);
Image image = Image.newImage(para, new URI("file:/c:/image.jpg"));
image.setTitle("Image title");
image.setDescription("This is a sample image");
image.setVerticalPosition(FrameVerticalPosition.TOP);
image.setHorizontalPosition(FrameHorizontalPosition.RIGHT);
image.setHyperlink(new URI("http://odftoolkit.org"));
FrameStyleHandler handler = image.getStyleHandler();
handler.setAchorType(AnchorType.AS_CHARACTER);
handler.setHorizontalRelative(HorizontalRelative.PAGE);
handler.setVerticalRelative(VerticalRelative.PAGE);
TextNavigation navigation = new TextNavigation("test", doc);
TextSelection sel = (TextSelection) navigation.nextSelection();
Span span = Span.newSpan(sel);
DefaultStyleHandler styleHandler = span.getStyleHandler();
Font font1Base = new Font("Arial", FontStyle.ITALIC, 10, Color.BLACK, TextLinePosition.THROUGH);
styleHandler.getTextPropertiesForWrite().setFont(font1Base);
span.applyHyperlink(new URI("ftp://ftpserver"));
TextDocument newDoc = TextDocument.newTextDocument();
//add a page break at the end of document. newDoc.addPageBreak();
//add a page break at the end of reference paragraph. Paragraph refParagraph = newDoc.addParagraph("before page break");
newDoc.addPageBreak(refParagraph);
TextDocument newDoc = TextDocument.newTextDocument();
MasterPage master = (MasterPage) getOrCreateMasterPage(newDoc, "Envelope");
Paragraph refParagraph = newDoc.addParagraph("before page break");
newDoc.addPageBreak(refParagraph, master);
TextDocument newDoc = TextDocument.newTextDocument();
MasterPage master = (MasterPage) getOrCreateMasterPage(newDoc, "Landscape");
//create a customized page style with specified width, height, margins and other properties. maste.setPageWidth(279.4);
master.setPageHeight(215.9);
master.setNumberFormat(NumberFormat.HINDU_ARABIC_NUMBER.toString());
master.setPrintOrientation(PrintOrientation.LANDSCAPE);
master.setFootnoteMaxHeight(0);
master.setWritingMode(StyleTypeDefinitions.WritingMode.LRTB);
master.setMargins(20, 20, 20, 20);
master.setFootnoteSepProperties(AdjustmentStyle.LEFT, Color.valueOf("#000000"), 1, 1, null, Percent.valueOf("25%"), 0.18);
Paragraph refParagraph = newDoc.addParagraph("before page break");
newDoc.addPageBreak(refParagraph, master);
TextTableOfContentElement textTableOfContentElement = doc.createDefaultTOC(paragraph1,false);
TOCStyle tocstyle = new TOCStyle(); tocstyle.addStyle("User_20_Index_20_1", 1); tocstyle.addStyle("User_20_Index_20_2", 2); tocstyle.addStyle("User_20_Index_20_3", 3); tocstyle.addStyle("User_20_Index_20_4", 4); tocstyle.addStyle("User_20_Index_20_5", 5); tocstyle.addStyle("User_20_Index_20_6", 6); tocstyle.addStyle("User_20_Index_20_7", 7); tocstyle.addStyle("User_20_Index_20_8", 8); tocstyle.addStyle("User_20_Index_20_9", 9); tocstyle.addStyle("User_20_Index_20_10", 10); TextTableOfContentElement textTableOfContentElement = doc.createTOCwithStyle(paragraph1, tocstyle, true);